﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using TrainingManagement.Models;

namespace TrainingManagement.Controllers
{
    public class CourseTimeUnitAPIController : ApiController
    {
        private trainingDBEntities db = new trainingDBEntities();

        // GET api/CourseTimeUnitAPI
        public IEnumerable<CourseTimeUnit> GetCourseTimeUnits()
        {
            return db.CourseTimeUnits.AsEnumerable();
        }

        // GET api/CourseTimeUnitAPI/5
        public CourseTimeUnit GetCourseTimeUnit(int id)
        {
            CourseTimeUnit coursetimeunit = db.CourseTimeUnits.Single(c => c.ID == id);
            if (coursetimeunit == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return coursetimeunit;
        }

        // PUT api/CourseTimeUnitAPI/5
        public HttpResponseMessage PutCourseTimeUnit(int id, CourseTimeUnit coursetimeunit)
        {
            if (ModelState.IsValid && id == coursetimeunit.ID)
            {
                db.CourseTimeUnits.Attach(coursetimeunit);
                db.ObjectStateManager.ChangeObjectState(coursetimeunit, EntityState.Modified);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/CourseTimeUnitAPI
        public HttpResponseMessage PostCourseTimeUnit(CourseTimeUnit coursetimeunit)
        {
            if (ModelState.IsValid)
            {
                db.CourseTimeUnits.AddObject(coursetimeunit);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, coursetimeunit);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = coursetimeunit.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/CourseTimeUnitAPI/5
        public HttpResponseMessage DeleteCourseTimeUnit(int id)
        {
            CourseTimeUnit coursetimeunit = db.CourseTimeUnits.Single(c => c.ID == id);
            if (coursetimeunit == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.CourseTimeUnits.DeleteObject(coursetimeunit);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, coursetimeunit);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}